#include "loginwgt.h"
#include <QPushButton>
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QLabel>
#include <QHBoxLayout>
#include <QLineEdit>
#include "styleloader.h"
#include "ICMS.h"
#include "appconfig.h"
Loginwgt::Loginwgt(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);
    setFixedSize(300,400);
    initLayout();
}

void Loginwgt::slot_Operator(const REQUESTTYPE type, bool result)
{
    switch (type) {
    case REGISTER:
        handleRegister(result);
        break;
    case LOGIN:
        handlelogin(result);
        break;
    default:
        break;

    }
}

void Loginwgt::slot_Mediainfo(const QVector<QVariantMap> mediainfo)
{
    //
    if(!mediainfo.isEmpty())
    {
        ICMS* icms = new ICMS();
        icms->show();
        hide();
    }
}

void Loginwgt::mouseMoveEvent(QMouseEvent *event)
{
    if(event->button() &  Qt::LeftButton && isPressed);
    {
        if(!qobject_cast<QPushButton*>(childAt(event->pos())))
        {
            move(event->globalPos()-point);
        }
    }
    QWidget::mouseMoveEvent(event);
}

void Loginwgt::mousePressEvent(QMouseEvent *event)
{
    if(!qobject_cast<QPushButton*>(childAt(event->pos())))
    {
        isPressed = true;
        point = event->globalPos()- this->frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}

void Loginwgt::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        isPressed = false;
    }
    QWidget::mouseReleaseEvent(event);
}

void Loginwgt::handlelogin(bool result)
{
    if(result)
    {
        AppConfig* app = AppConfig::instance();
        HttpHelper* httpHelper = HttpHelper::instance();
        QVector<QVariantMap> infos = app->getVideoFiles();
        for(const QVariantMap& video : infos)
        {
            if(video.contains("mediaUrl")&& video.contains("mediaTitle"))
            {
                QString mediaUrl = video["mediaUrl"].toString();
                QString mediaTitle = video["mediaTitle"].toString();
                httpHelper->slot_AddmediaInfo(video);
                qDebug() <<"媒体文件" <<mediaTitle<<"路径："<<mediaUrl;
            }
            else
            {
                qDebug() <<"缺少必要字段";
            }
        }
        httpHelper->slot_GetmadiaInfo();
    }
    else
    {
        qDebug() <<"登录失败";
    }
}

void Loginwgt::handleRegister(bool result)
{
    if(result)
    {
        stackedWgt->setCurrentWidget(loginWgt);
    }
    else
    {
        qDebug()<<"注册失败";
    }
}

void Loginwgt::initLayout()
{
    //主布局
    QVBoxLayout* mainlayout = new QVBoxLayout(this);
    //创建公共窗口（标题窗口，用于关闭程序）
    QWidget* headerWgt = new QWidget(this);
    headerWgt->setFixedSize(300,150);
    headerWgt->setWindowFlag(Qt::FramelessWindowHint);
    headerWgt->setAttribute(Qt::WA_StyledBackground);

    //登录用户头像
    QLabel* iconlabel = new QLabel(headerWgt);
    iconlabel->setAlignment(Qt::AlignCenter);
    QPushButton* closeBtn = new QPushButton(headerWgt);
    iconlabel->setFixedSize(80,80);
    closeBtn->setFixedSize(30,30);

    //头像设置图片
    iconlabel->setPixmap(QPixmap("qrc:/../../../../7d8eea67e76189d9.jpg"));
    //图片缩放
    iconlabel->setScaledContents(true);

    //关闭按钮绑定信号与槽
    connect(closeBtn,&QPushButton::clicked,this,[this](){
        close();
    });

    //headerWgt布局
    QVBoxLayout* headerlayout = new QVBoxLayout(headerWgt);
    headerlayout->setContentsMargins(0,0,0,0);
    headerlayout->setSpacing(0);

    //顶部水平布局
    QHBoxLayout* toplayout = new QHBoxLayout();
    toplayout->setContentsMargins(0,0,0,0);
    toplayout->addStretch();
    toplayout->addWidget(closeBtn);
    headerlayout->addLayout(toplayout);

    //左键间隔
    headerlayout->addStretch();

    //图标布局
    QHBoxLayout* iconlayout = new QHBoxLayout();
    iconlayout->setContentsMargins(0,0,0,0);
    iconlayout->addStretch();
    iconlayout->addWidget(iconlabel);
    iconlayout->addStretch();
    headerlayout->addLayout(iconlayout);

    headerWgt->setLayout(headerlayout);

    //stackWidget
    stackedWgt = new QStackedWidget(this);
    stackedWgt->setFixedSize(300,250);
    stackedWgt->setWindowFlag(Qt::FramelessWindowHint);
    stackedWgt->setAttribute(Qt::WA_StyledBackground);

    mainlayout->setSpacing(0);
    mainlayout->addWidget(headerWgt);
    mainlayout->addWidget(stackedWgt);
    mainlayout->setContentsMargins(0,0,0,5);

    //登录窗口
    loginWgt = new QWidget(stackedWgt);
    registerWgt = new QWidget(stackedWgt);

    loginWgt->setFixedSize(300,250);
    loginWgt->setWindowFlag(Qt::FramelessWindowHint);
    loginWgt->setAttribute(Qt::WA_StyledBackground);

    registerWgt->setFixedSize(300,250);
    registerWgt->setWindowFlag(Qt::FramelessWindowHint);
    registerWgt->setAttribute(Qt::WA_StyledBackground);

    //添加登录窗口子文件
    QLineEdit* account = new QLineEdit(loginWgt);
    QLineEdit* passwd = new QLineEdit(loginWgt);
    QPushButton* loginBtn = new QPushButton("登录",loginWgt);
    QPushButton* registerBtn = new QPushButton("没有账号？去注册",loginWgt);

    account->setFixedSize(250,40);
    passwd->setFixedSize(250,40);
    loginBtn->setFixedSize(250,40);
    registerBtn->setFixedSize(250,40);

    account->setPlaceholderText("username");
    passwd->setPlaceholderText("passwd");
    passwd->setEchoMode(QLineEdit::Password);

    //登录窗口子控件布局
    QVBoxLayout* loginlayout = new QVBoxLayout(loginWgt);
    loginlayout->setAlignment(Qt::AlignHCenter);
    loginlayout->setSpacing(10);
    loginlayout->addWidget(account);
    loginlayout->addWidget(passwd);
    loginlayout->addStretch();
    loginlayout->addWidget(loginBtn);
    loginlayout->addWidget(registerBtn);
    loginlayout->addStretch();
    loginlayout->setContentsMargins(0,0,0,0);
    loginWgt->setLayout(loginlayout);
    //注册子控件
    QLineEdit* reusername = new QLineEdit(registerWgt);
    QLineEdit* repasswd = new QLineEdit(registerWgt);
    QPushButton* startRegisterBtn = new QPushButton("开始注册",registerWgt);
    QPushButton* reloginBtn = new QPushButton("返回登录",registerWgt);

    connect(loginBtn,&QPushButton::clicked,this,[=](){
       QVariantMap infoMap;
       QString username = account->text();
       QString userpasswd = passwd->text();
       if(!username.isEmpty() && !userpasswd.isEmpty())
       {
           infoMap["userName"] = username;
           infoMap["password"] = userpasswd;
           infoMap["nickName"] = "test";
           HttpHelper::instance()->slot_Login(infoMap);
       }
       else
       {
           qDebug() <<"账号密码不能为空";
       }
    });

    connect(startRegisterBtn,&QPushButton::clicked,this,[=](){
       QVariantMap infoMap;
       QString username = reusername->text();
       QString userpasswd = repasswd->text();
       if(!username.isEmpty() && !userpasswd.isEmpty())
       {
           infoMap["userName"] = username;
           infoMap["password"] = userpasswd;
           infoMap["nickName"] = "test";
           HttpHelper::instance()->slot_Login(infoMap);
       }
       else
       {
           qDebug() <<"账号密码不能为空";
       }
    });

    connect(reloginBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(loginWgt);
    });

    //设置注册窗口子控件大小
    reusername->setFixedSize(250,40);
    repasswd->setFixedSize(250,40);
    startRegisterBtn->setFixedSize(250,40);
    reloginBtn->setFixedSize(250,40);

    //编辑框设置占位符
    reusername->setPlaceholderText("reusername");
    repasswd->setPlaceholderText("repasswd");
    repasswd->setEchoMode(QLineEdit::Password);

    //注册窗口设置布局
    QVBoxLayout* registerlayout = new QVBoxLayout(registerWgt);
    registerlayout->setAlignment(Qt::AlignHCenter);
    registerlayout->setSpacing(10);
    registerlayout->addWidget(reusername);
    registerlayout->addWidget(repasswd);
    registerlayout->addStretch();
    registerlayout->addWidget(startRegisterBtn);
    registerlayout->addWidget(reloginBtn);
    registerlayout->addStretch();
    registerlayout->setContentsMargins(0,0,0,0);
    registerWgt->setLayout(registerlayout);

    //stackedwidget添加子窗口
    stackedWgt->addWidget(loginWgt);
    stackedWgt->addWidget(registerWgt);

    connect(registerBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(registerWgt);
    });

    stackedWgt->setCurrentWidget(loginWgt);

    this->setLayout(mainlayout);







}
